#+---------------------------------------------+
#| ProxyBot 0.7
#+---------------------------------------------+
#| Written by Alec Hussey
#| Copyright (C) 2008 MadSoft
#| Website: www.madsoft.org
#| License: GNU General Public License v3
#+---------------------------------------------+

class Database:
	def __init__(self, dbtype, dbhost, dbname, dbuser=None, dbpass=None):
		# Determine database type and import appropriate module
		if dbtype == "mysql":
			exec("import MySQLdb as db")
		elif dbtype == "postgresql":
			exec("import psycopg2 as db")
		elif dbtype == "sqlite":
			exec("import sqlite3 as db")
		else:
			raise RuntimeError, "Invalid database type specified."
		
		# Make connection to the database
		try:
			if dbtype == "mysql":
				self.connection = db.connect(host=dbhost, user=dbuser, passwd=dbpass, db=dbname)
			elif dbtype == "postgresql":
				self.connection = db.connect("host='%s' dbname='%s' user='%s' password='%s'"
					% (dbhost, dbname, dbuser, dbpass))
			elif dbtype == "sqlite":
				self.connection = db.connect(dbname)
			
			self.cursor = self.connection.cursor()
		except:
			raise RuntimeError, "Failed to connect to database."
